function isLogin() {
    return JSON.parse(localStorage.getItem('token'))
}

function renderTopbar(user) {
    let photo = document.getElementById('topbar-photo')
    let currentUsername = document.getElementById('currentUsername')
    let exit = document.getElementById('exit')
    console.log(user)
    photo.src = user.photo
    currentUsername.innerHTML = user.username
    exit.onclick = function () {
        localStorage.removeItem('token')
        location.href = '/admin/views/login/login.html'
    }
}
function renderSidemenu(user, id) {
    let highlight = document.querySelector('#' + id);
    highlight.style.color = '#0d6efd'
    if (user.role !== 'admin') {
        // usermanageitem.removeItem()
        document.querySelector('#usermanageitem').remove()
    }
}

async function load(id) {
    let user = isLogin()
    if (user) {
        let topbarText = await fetch('/admin/components/topbar/topbar.html').then(res => res.text())
        document.querySelector('.topbar').innerHTML = topbarText
        renderTopbar(user)
        let sidemenu = await fetch('/admin/components/sidemenu/sidemenu.html').then(res => res.text())
        document.querySelector('.sidemenu').innerHTML = sidemenu
        renderSidemenu(user, id)
    } else {
        location.href = '/admin/views/login/login.html'
    }
}

export { load, isLogin } 